package com.browsesoft.oa;

import java.io.File;
import java.util.Iterator;
import java.util.LinkedList;

import com.browsesoft.EntityManager;
import com.browsesoft.PropertiesService;
import com.browsesoft.Tools;
import com.browsesoft.baseadapter.SimpleEntity;
import com.browsesoft.dbtools.DBTools;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.parameter.Parameter;
import com.browsesoft.parameter.ParameterValue;
import com.browsesoft.resource.BasicResource;
import com.browsesoft.user.User;

/**
 * 批量删除类：：单例模式
 */
public class MoreDelete {
	/**
	 * 批量删除类自己
	 */
	private static MoreDelete self = new MoreDelete();

	/**
	 * 风格文件路径
	 */
	private String path = "";

	/**
	 * 构造函数
	 * 
	 */
	public MoreDelete() {
		try {
			self = this;
			if (path != null && path.equals("")) {
				path = PropertiesService.getProperty("css", "path");
			}
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}

	/**
	 * 得到管理器自己
	 */
	public static MoreDelete getinstance() {
		return self;
	}

	/**
	 * 参数删除
	 */
	public void paramDelete(LinkedList changed, User user,
			HTMLBasicComponent comp) throws Exception {
		Iterator iter = changed.iterator();
		String id = "";
		while (iter.hasNext()) {
			id = (String) iter.next();
			// 删除该id对应的参数
			this.deleteParamter(id, comp);
		}
		String onload = comp.getPage().getElement().getAttribute("onload");
		if (onload != null && !onload.equals("")) {
			comp.getPage().showMessage(onload + ";document.forms[0].submit()");
		} else {
			comp.getPage().showMessage("document.forms[0].submit()");
		}
	}

	/**
	 * 参数值删除
	 */
	public void parameterValueDelete(LinkedList changed, HTMLBasicComponent comp)
			throws Exception {
		Iterator iter = changed.iterator();
		String message = "";
		while (iter.hasNext()) {
			String id = (String) iter.next();
			ParameterValue paValue = new ParameterValue(id);
			// 根据ID得到参数
			Parameter pa = paValue.getParameter();
			if (pa.getAttributes().get("name").equals("联系人类别")) {
				// 得到所有联系人类别
				String linkname = this.getLinkMan();
				String name = (String) paValue.getAttributes().get("name");
				if (linkname.indexOf(name) != -1) {
					message += name + ",";
					message = "参数值" + message + "正在使用，不能删除！";
				} else {
					paValue.delete();
				}
			} else {
				paValue.delete();
			}
		}
		if (message != null && !message.equals("")) {
			comp.getPage().showMessage(
					"alert('" + message + "');document.forms[0].submit()");
		} else {
			comp.getPage().showMessage("document.forms[0].submit();");
		}
	}

	public void deleteParamter(String id, HTMLBasicComponent comp)
			throws Exception {
		// 根据ID得到参数
		Parameter parameter = new Parameter();
		parameter.getParameters(id);
		// 参数值的删除
		this.deleteParamterValue(parameter, comp);
		if (parameter.getAttributes().get("name").equals("联系人类别")) {
			LinkedList list = parameter.getParamValues();
			if (list.size() == 0) {
				parameter.delete();
			}
		} else {
			parameter.delete();
		}
	}

	public void deleteParamterValue(Parameter parameter, HTMLBasicComponent comp)
			throws Exception {
		String linkemanClass = "";
		String name = (String) parameter.getAttributes().get("name");
		if (name != null && name.equals("联系人类别")) {
			// 得到所有联系人类别
			linkemanClass = this.getLinkMan();
			if (linkemanClass != null && !linkemanClass.equals("")) {
				String message = "联系人类别中的" + linkemanClass + "正在使用，不能删除！";
				comp.getPage().showMessage("alert('" + message + "')");
			}
		}
		LinkedList temp = parameter.getParamValues();
		Iterator iter = temp.iterator();
		ParameterValue paValue = null;
		String pavalue = null;
		while (iter.hasNext()) {
			paValue = (ParameterValue) iter.next();
			pavalue = (String) paValue.getAttributes().get("name");
			if (linkemanClass.indexOf(pavalue) == -1) {
				paValue.delete();
			}
		}
	}

	/**
	 * 得到所有联系人类别
	 */
	public String getLinkMan() throws Exception {
		String linkemanClass = "";
		String f_class = "";
		Iterator iter = EntityManager.getInstance().getObjects(
				"com.browsesoft.baseadapter.SimpleEntity", "entitytype",
				"personaladdress").iterator();
		while (iter.hasNext()) {
			SimpleEntity smp = (SimpleEntity) iter.next();
			f_class = (String) smp.getAttributes().get("f_class");
			if (linkemanClass.indexOf(f_class) == -1) {
				linkemanClass += " " + f_class + " ";
			}
		}
		return linkemanClass;
	}

	/**
	 * 实体的删除
	 */
	public void deleteSelected(LinkedList changed, User user,
			HTMLBasicComponent comp) throws Exception {
		Iterator iter = changed.iterator();
		// 对于每一个实体
		while (iter.hasNext()) {
			String id = (String) iter.next();
			SimpleEntity entity = (SimpleEntity) EntityManager.getInstance()
					.getObject(id);
			// 如果不为空
			if (entity != null) {
				entity.setLoginUser(user);
				this.delete(entity, comp);
			}
		}
	}

	/**
	 * 删除传递对象::状态为结束的才可以删除
	 * 
	 * @param changed
	 *            传递对象号
	 * @param user
	 *            为了记录删除日志
	 * @param comp
	 *            为了提示信息
	 */
	public void deleteTransfer(LinkedList changed, String tableName, User user,
			HTMLBasicComponent comp) throws Exception {
	}

	/**
	 * 新闻删除
	 */
	public void deletedSelectNews(LinkedList changed, User user)
			throws Exception {
	}

	/**
	 * 资源的删除
	 */
	public void deleteSelectedResource(LinkedList changed, User user,
			HTMLBasicComponent comp) throws Exception {
		Iterator iter = changed.iterator();
		// 对于每一个实体
		while (iter.hasNext()) {
			String id = (String) iter.next();
			BasicResource entity = (BasicResource) EntityManager.getInstance()
					.getObject(id);
			// 如果不为空
			if (entity != null) {
				Iterator linkedList = entity.getChildren();
				LinkedList newList = Tools.iteratorToList(linkedList);
				if (linkedList.hasNext()) {
					this.deleteResource(newList, user);
				}
				entity.setLoginUser(user);
				entity.delete();
			}
		}
	}

	private void deleteResource(LinkedList list, User user) throws Exception {
		for (int i = 0; i < list.size(); i++) {
			BasicResource resource = (BasicResource) list.get(i);
			LinkedList ls = Tools.iteratorToList(resource.getChildren());
			if (ls.size() > 0) {
				this.deleteResource(ls, user);
			}
			resource.setLoginUser(user);
			resource.delete();
		}
	}

	/**
	 * 根据不同实体进行不同删除操作
	 * 
	 * @param entity
	 *            实体 一般实体：：删除 风格实体：：删除服务器中风格文件 授权策略：：不能删除最高授权记录
	 */
	public void delete(SimpleEntity entity, HTMLBasicComponent comp)
			throws Exception {
		// 得到实体类型
		String entityType = (String) entity.getAttributes().get("entitytype");
		// 根据不同实体类型进行不同删除
		// 风格
		if (entityType != null && entityType.equals("personalstyle")) {
			this.doCssDelete(entity);
		}
		// 授权策略
		else if (entityType != null && entityType.equals("licensepolicy")) {
			this.doLicenseDelete(entity, comp);
		} else {
			this.doEntityDelete(entity);
		}
	}

	/**
	 * 资源删除
	 * 
	 */
	public void resourceDelete() {
	}

	/**
	 * 风格实体删除
	 */
	private void doCssDelete(SimpleEntity entity) throws Exception {
		// 得到实体对应文件
		String fileName = (String) entity.getAttributes().get(
				"f_stylesheetname");
		String filePath = path + "/" + fileName;
		File file = new File(filePath);
		if (file.exists()) {
			file.delete();
		}
		this.doEntityDelete(entity);
	}

	/**
	 * 授权策略删除
	 */
	private void doLicenseDelete(SimpleEntity entity, HTMLBasicComponent comp)
			throws Exception {
		// 得到实体
		String licensepolicy = (String) entity.getAttributes().get(
				"licensepolicy");
		if (licensepolicy != null
				&& licensepolicy.indexOf("统一资源管理(网络)  及其所有子") != -1) {
			String message = licensepolicy + "不能删除！";
			comp.getPage().showMessage("alert('" + message + "')");
		} else {
			this.doEntityDelete(entity);
		}
	}

	/**
	 * 论坛板块删除
	 */
	public void forumDelete(LinkedList changed) throws Exception {
	}

	/**
	 * 论坛帖子删除
	 */
	public void postDelete(LinkedList changed) throws Exception {
	}

	/**
	 * 实体删除
	 */
	private void doEntityDelete(SimpleEntity entity) throws Exception {
		entity.delete();
	}

	/**
	 * 邮件彻底删除
	 */
	public void deleteMailS(LinkedList selected) throws Exception {
	}

	/**
	 * 实体的删除
	 */
	public void deletePosts(LinkedList datas, LinkedList changed, User user,
			HTMLBasicComponent comp) throws Exception {
		// 为了删除选中得实体
		Iterator iter = changed.iterator();
		while (iter.hasNext()) {
			// 根据id号得到实体
			String id = (String) iter.next();
			SimpleEntity entity = (SimpleEntity) EntityManager.getInstance()
					.getObject(id);
			String entityType = "";
			// 得到该实体得实体类型
			if (entity.getAttributes().get("entitytype") != null) {
				entityType = (String) entity.getAttributes().get("entitytype");
			}
			// 如果是主题
			if (entityType.equals("oatheme")) {
				// 只调用实体的删除
				entity.delete();
				datas.clear();
				return;
			}
			// 如果是帖子
			else {
				datas.remove(entity);
				entity.delete(true);
			}
		}
	}
}